package com.jie.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jie.dto.ClockPlanDTO;
import com.jie.entity.ClockPlan;
import com.jie.entity.CombinationTopic;
import com.jie.mapper.ClockPlanMapper;
import com.jie.mapper.CombinationTopicMapper;
import com.jie.service.ClockPlanService;
import com.jie.utils.BeanCopyUtils;
import com.jie.utils.UserUtils;
import com.jie.vo.ClockPlanVO;
import com.jie.vo.ConditionVO;
import com.jie.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;


@Service
public class ClockPlanServiceImpl extends ServiceImpl<ClockPlanMapper, ClockPlan> implements ClockPlanService {
    @Autowired
    private CombinationTopicMapper combinationTopicMapper;

    @Override
    public void saveClockPlan(ClockPlanVO clockPlanVO) {
        //这里需要注意，用户id在这里处理来获取，VO中不带此信息
        ClockPlan clockPlan = ClockPlan.builder()
                .date(LocalDateTime.now())
                .userId(UserUtils.getLoginUser().getId())  //使用UserUtils类来获取用户信息
                .sportId(clockPlanVO.getSportId())
                .consumption(clockPlanVO.getConsumption())
                .planName(clockPlanVO.getPlanName())
                .startTime(clockPlanVO.getStartTime())
                .endTime(clockPlanVO.getEndTime())
                .build();
        //定义消耗量
        if(clockPlan.getConsumption() == null){
            if(clockPlan.getSportId() != null){
                //根据sportId从表中查询对应的运动素材信息,
                CombinationTopic combinationTopic = combinationTopicMapper.selectById(clockPlan.getSportId());
                //获取大卡信息
                if(combinationTopic != null)clockPlan.setConsumption(combinationTopic.getTopicConsumable());
                // 若video为空如何处理后续拓展
            }
        }
        //根据开始和结束时间定义运动时长
        Duration duration = Duration.between(clockPlan.getStartTime(), clockPlan.getEndTime());
        long minutes = duration.toMinutes();
        clockPlan.setExerciseDuration((int) minutes);

        this.baseMapper.insert(clockPlan);
    }

    @Override
    public Integer deleteById(Integer id) {
        this.baseMapper.deleteById(id);
        return id;
    }

    @Override
    public ClockPlan selectById(Integer id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public PageResult<ClockPlanDTO> listClockPlans(ConditionVO conditionVO) {
        conditionVO.setCurrent(conditionVO.getCurrent() > 0 ? conditionVO.getCurrent() : 1);
        conditionVO.setSize(conditionVO.getSize() > 0 ? conditionVO.getSize() : 10);
        Page<ClockPlan> page = new Page<>(conditionVO.getCurrent(), conditionVO.getSize());
        // 查询日志列表
        Page<ClockPlan> ClockPlanPage = this.page(page, new LambdaQueryWrapper<ClockPlan>()
                .like(StringUtils.isNotBlank(conditionVO.getKeywords()), ClockPlan::getPlanName, conditionVO.getKeywords())
                .or()
                .like(StringUtils.isNotBlank(conditionVO.getKeywords()), ClockPlan::getConsumption, conditionVO.getKeywords())
                .orderByDesc(ClockPlan::getId));
        List<ClockPlanDTO> operationLogDTOList = BeanCopyUtils.copyList(ClockPlanPage.getRecords(), ClockPlanDTO.class);
        return new PageResult<>(operationLogDTOList, operationLogDTOList.size());
    }
}
